var abnormalStatisticsData = [];
var chargingStationData = [];
var totalPage = 0;
var totalCnt = 0;
var orderList = [];
var lastheaderNameEN = undefined;
var flagDate = undefined;
var formData = undefined;

//查询
$(document).ready(function() {
	showLoading();
	$('title', parent.document).html($('title').text());
	setBaseUrl();
	createTableHead("chargingRecordStatisticsThead", abnormalStatisticsTable, search);
	createTableHead("stationStatisticsThead", stationStatisticsTable);
	scrollTableSuspend("scrollBody", "stationStatisticsTable", "stationStatisticsTable1", "stationStatisticsThead", "scrollHeight");
	scrollTableSuspend("scrollBody", "chargingRecordTable", "chargingRecordTable1", "chargingRecordStatisticsThead", "scrollHeight");
	selectDate("startDate", 0);
	selectDate("endDate", 0);
	$ajaxList('chargingStation', getChargingStation, {BulidStatus: '3'});
	changeMonth(0);
});
function getChargingStation(data) {
	var length = data.length;
	chargingStationData = data;
	for (var i = 0; i < length; i++) {
		$("#chargingStationID").append("<option value='" + data[i].chargingStationID + "'>" + data[i].chargingStationName + "</option>");
	}
}
function changeEquipmet(){
	var chargingStationID = $("#chargingStationID").val();
	if (chargingStationID) {
		$("#changeEquipment").show();
		$("#equipmentNo").empty();
		$ajaxList('equipmentNo', getEquipmentNo, {"chargingStationID": chargingStationID});
	} else {
		$("#changeEquipment").hide();
		$("#equipmentNo").empty();
	}
}
function getEquipmentNo(data) {
	var length = data.length;
	$("#equipmentNo").append("<option value=''>请选择</option>");
	for (var i = 0; i < length; i++) {
		$("#equipmentNo").append("<option value='" + data[i].equipmentNo + "'>" + data[i].equipmentName + "</option>");
	}
	if(formData){
		if(formData.equipmentNo != "") {
			$("#equipmentNo").val(formData.equipmentNo);
		}
	}
}
function showDay(index, date){
	$('.activeLi').removeClass("activeLi");
	$("#li"+ index).addClass("activeLi");
	var nowDate = stringToDate(date);
	nowDate.setDate(index);
	$("#startChargingTime").val(date2String(nowDate) + " 00:00:00");
	$("#endChargingTime").val(date2String(nowDate) + " 23:59:59");
	search();
}
function showForm(){
	if(document.getElementById("hideform").hidden){
		$("#hideform").attr("hidden", false);
	} else{
		$("#hideform").attr("hidden", true);
	}	
}
function search(headerNameEN){
	if($("#detailID", parent.document).val() == "backFlag"){
		$("#searchType").val("detail");
		$("#detailID", parent.document).val("");
	}
	var startChargingTime = $("#startChargingTime").val();
	var endChargingTime = $("#endChargingTime").val();
	if(lastheaderNameEN != headerNameEN){
		clearOrder(lastheaderNameEN);
		lastheaderNameEN = headerNameEN;
	}
	if (stringToDate(startChargingTime) > stringToDate(endChargingTime)){
		showMessge("开始时间不能大于结束时间！");
	} else {
		$ajax('searchAbnormalStatistics', 'chargingRecordStatisticsSearch', searchChargingRecordStatistics);
	}
}
function searchChargingRecordStatistics(data) {
	totalPage = data && data.totalPage || 0;
	totalCnt = data && data.totalCnt || 0;
	if(data.chargingRecord){
		formData = data && data.chargingRecord;
		if(formData.chargingStationID != "") {
			$("#chargingStationID").val(formData.chargingStationID);
			changeEquipmet();
		}
		if(formData.startChargingTime != "") {
			$("#startChargingTime").val(formData.startChargingTime);
			$('.activeLi').removeClass("activeLi");
			var array = formData.startChargingTime.split(/[^0-9]/ig);
			$("#li"+ parseInt(array[2])).addClass("activeLi");
		}
		if(formData.endChargingTime != "") {
			$("#endChargingTime").val(formData.endChargingTime);
		}
		$("#searchType").val("");
	}
	abnormalStatisticsData = data && data.chargingRecordStatisticsList || [];
	createTableBody("chargingRecordStatisticsTbody", abnormalStatisticsTable, abnormalStatisticsData);
	var totalBalanceAmmeterNo = 0;
	var totalChargingCount = 0;
	var totalChargingKWH = 0;
	var totalAttritionKWH = 0;
	var totalLossCount = 0;
	var totalAbnormalCount = 0;
	var totalAbnormalChargingKWH = 0;
	for(var i = 0; i < abnormalStatisticsData.length; i++){
		totalBalanceAmmeterNo += abnormalStatisticsData[i].balanceAmmeterNo;
		totalChargingCount += abnormalStatisticsData[i].chargingCount;
		totalChargingKWH += abnormalStatisticsData[i].totalChargingKWH;
		totalAttritionKWH += abnormalStatisticsData[i].attritionAmmeterNo;
		totalLossCount += abnormalStatisticsData[i].lossCount;
		totalAbnormalCount += abnormalStatisticsData[i].abnormalCount;
		totalAbnormalChargingKWH += abnormalStatisticsData[i].abnormalChargingKWH;
		$("#abnormalReason" + i).html(getAbnormalReason(abnormalStatisticsData[i].abnormalReason));
		$("#abnormalRate" + i).html((abnormalStatisticsData[i].abnormalCount/(abnormalStatisticsData[i].abnormalCount + abnormalStatisticsData[i].chargingCount)).toFixed(4));
		if(abnormalStatisticsData[i].lossCount > 0){
			$("#lossCount" + i).html('<a onclick="lossDetail(' + i+ ')" title="点击查看详细">' + abnormalStatisticsData[i].lossCount + " 详情" + '</a>');
			$("#attritionAmmeterNo" + i).css("color", "red");
		}
		if(abnormalStatisticsData[i].abnormalCount > 0){
			$("#abnormalCount" + i).html('<a onclick="details(' + i+ ')" title="点击查看详细">' + abnormalStatisticsData[i].abnormalCount + " 详情" + '</a>');
			$("#abnormalChargingKWH" + i).css("color", "red");
		}
	}
	var totalAbnormalRate = (totalAbnormalCount/(totalAbnormalCount + totalChargingCount)).toFixed(4) || 0;
	if(document.getElementById('chargingRecordStatisticsThead').rows.length>1){
    	document.getElementById('chargingRecordStatisticsThead').deleteRow(1);
    }
	$('#chargingRecordStatisticsThead').append('<tr><th>合计</th><th></th><th></th><th></th><th></th><th>' + totalBalanceAmmeterNo + '</th><th>' + totalChargingCount + '</th><th>' + totalChargingKWH.toFixed(0) + '</th><th>' + totalAbnormalCount + '</th><th>' + totalAbnormalChargingKWH.toFixed(0) + '</th><th>' + totalAbnormalRate + '</th><th></th><th>' + totalLossCount + '</th><th>' + totalAttritionKWH.toFixed(0) + '</th><th></th><th></th></tr>');
	stationStatistics();
	showAbnormal();
}
function stationStatistics(){
	var stationStatisticsData = [];
	var length = abnormalStatisticsData.length || 0;
	for(var i = 0; i < chargingStationData.length; i++){
		var stationData = new Object();
		var chargingStationName = "";
		var balanceAmmeterNo = 0;
		var chargingCount = 0;
		var totalChargingKWH = 0;
		var lossCount = 0;
		var abnormalCount = 0;
		var abnormalChargingKWH = 0;
		var abnormalRate = 0;
		var attritionAmmeterNo = 0;
		for(var j = 0; j < length; j++){
			if(abnormalStatisticsData[j].chargingStationID == chargingStationData[i].chargingStationID){
				chargingStationName = abnormalStatisticsData[j].chargingStationName;
				balanceAmmeterNo += abnormalStatisticsData[j].balanceAmmeterNo;
				chargingCount += abnormalStatisticsData[j].chargingCount;
				totalChargingKWH += abnormalStatisticsData[j].totalChargingKWH;
				lossCount += abnormalStatisticsData[j].lossCount;
				abnormalCount += abnormalStatisticsData[j].abnormalCount;
				abnormalChargingKWH += abnormalStatisticsData[j].abnormalChargingKWH;
				attritionAmmeterNo += abnormalStatisticsData[j].attritionAmmeterNo;
			}
		}
		if(chargingStationName != ""){
			stationData.chargingStationName = chargingStationName;
			stationData.balanceAmmeterNo = balanceAmmeterNo;
			stationData.chargingCount = chargingCount;
			stationData.totalChargingKWH = totalChargingKWH;
			stationData.lossCount = lossCount;
			stationData.abnormalCount = abnormalCount;
			stationData.abnormalChargingKWH = abnormalChargingKWH;
			stationData.abnormalRate = (abnormalCount/(abnormalCount + chargingCount)).toFixed(4);
			stationData.attritionAmmeterNo = attritionAmmeterNo;
			stationData.attritionAmmeterRate = (attritionAmmeterNo/balanceAmmeterNo).toFixed(4);
			stationStatisticsData.push(stationData);
		}
	}
	createTableBody("stationStatisticsTbody", stationStatisticsTable, stationStatisticsData);
}
function getAbnormalReason(data){
	var array = repeat(data.split(","));
	var abnormalReason = "";
	for(var i = 0; i < array.length; i++){
		abnormalReason += "<div>" +array[i] + "</div>";
	}
	return abnormalReason;
}
function repeat(data) {
    var res = [], hash = {};
    for(var i=0, elem; (elem = data[i]) != null; i++)  {
        if (!hash[elem])
        {
            res.push(elem);
            hash[elem] = true;
        }
    }
    return res;
}
function showAbnormal(){
	var tbodtID = $('#chargingRecordStatisticsTbody');
    var rowsLength = tbodtID[0].childElementCount;
    var tbodyID = $('#stationStatisticsTbody');
    var length = tbodyID[0].childElementCount;
	if(!document.getElementById("showAbnormal").checked){
	    for(var i=0;i<rowsLength;i++){
	    	var showDisplay = false;
	    	if(tbodtID[0].rows[i].children[9].children["0"].style.color || tbodtID[0].rows[i].children[13].children["0"].style.color){
	    		showDisplay = true;
	    	}
	    	if(!showDisplay){
	    		tbodtID[0].rows[i].style.display='none';
	    	}
	    }
	    for(var i=0;i<length;i++){
	    	var display = false;
	    	if(tbodyID[0].rows[i].children[4].innerHTML > 0 || tbodyID[0].rows[i].children[7].innerHTML > 0){
	    		display = true;
	    	}
	    	if(!display){
	    		tbodyID[0].rows[i].style.display='none';
	    	}
	    }
	} else{
		for(var i=0;i<rowsLength;i++){
    		tbodtID[0].rows[i].style.display='';
	    }
		for(var i=0;i<length;i++){
			tbodyID[0].rows[i].style.display='';
	    }
	}
}
function chkBefore(index, chkFlag){
	if (chkFlag == "update") {
		update(index);
	}
}
function details(index){
	$("#detailID", parent.document).val(abnormalStatisticsData[index].equipmentNo);
	$("#startTime", parent.document).val($("#startChargingTime").val());
	$("#endTime", parent.document).val($("#endChargingTime").val());
	window.parent.detalisSkip('chargingEquipment/abnormalRecord');
}
function lossDetail(index){
	$("#detailID", parent.document).val(abnormalStatisticsData[index].equipmentNo);
	$("#startTime", parent.document).val($("#startChargingTime").val());
	$("#endTime", parent.document).val($("#endChargingTime").val());
	window.parent.detalisSkip('chargingEquipment/chargingRecordStatisticsDetail');
}
function update(index) {
	window.parent.detalisSkip('chargingEquipment/abnormalRecord', index, 1);
	$("#parentSkipMenu", parent.document).val("chargingRecordPage");
	$("#detailID", parent.document).val(abnormalStatisticsData[index].equipmentNo);
}